import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable, from } from "rxjs";
import { SubwarehouseManageService } from '../subwarehouse-manage/subwarehouse-manage.service'

@Injectable({
  providedIn: 'root'
})
export class WarehouseManageService {

  private managerListUrl = '/api/warehouseManager/only'
  private manageCKUrl = '/api/warehouseManager'

  // 库房库区列表
  getKFList(): Observable<any>{
    return this.SubwarehouseManageService.getKFAllList()
  }

  // 管理员列表
  async getManagerList(data): Promise<Node> {
    let dataJson = {
      page: data.page,
      size: data.size
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.get(this.managerListUrl, { params: dataJson }).subscribe((res: any) => {
        resolve(res);
      }, err => {
        reject(err)
      })
    })
  }
  // 管理员下的仓库列表
  async getManagerCKList(data): Promise<Node> {
    let dataJson = {
      managerId: data.managerId,
      page: data.page,
      size: data.size
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.get(this.manageCKUrl, { params: dataJson }).subscribe((res: any) => {
        resolve(res);
      }, err => {
        reject(err)
      })
    })
  }
  // 添加管理员下的仓库列表
  async addManagerCKList(data): Promise<Node> {
    let resources = {
      managerId: data.managerId,
      toWarehouseAreaId: data.toWarehouseAreaId,
      toWarehouseId: data.toWarehouseId,
      warehouseAreaId: data.warehouseAreaId,
      warehouseId: data.warehouseId,
      isDefault: data.isDefault
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.post(this.manageCKUrl, resources).subscribe((res: any) => {
        resolve(res);
      }, err => {
        reject(err)
      })
    })
  }
  // 修改管理员下的仓库列表
  async editManagerCKList(data): Promise<Node> {
    let resources = {
      id:data.id,
      managerId: data.managerId,
      toWarehouseAreaId: data.toWarehouseAreaId,
      toWarehouseId: data.toWarehouseId,
      warehouseAreaId: data.warehouseAreaId,
      warehouseId: data.warehouseId,
      isDefault: data.isDefault
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.put(this.manageCKUrl, resources).subscribe((res: any) => {
        resolve(res);
      }, err => {
        reject(err)
      })
    })
  }
  // 删除管理员下的仓库列表
  async deleteManageCK(data): Promise<Node> {
    let options = {
      headers: {
        "Content-Type":"application/json"
      },
      body: data
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.delete(this.manageCKUrl, options).subscribe((res: any) => {
        resolve(res);
      }, err => {
        reject(err)
      })
    })
  }

  constructor(
    private http: HttpClient,
    private SubwarehouseManageService:SubwarehouseManageService
  ) { }
}
